<template>
  <basic-container>
    <avue-crud :option="option" :table-loading="loading" :data="data" :page="page" :permission="permissionList"
               :before-open="beforeOpen" v-model="form" ref="crud" @row-update="rowUpdate" @row-save="rowSave"
               @row-del="rowDel"
               @search-change="searchChange" @search-reset="searchReset" @selection-change="selectionChange"
               @current-change="currentChange" @size-change="sizeChange" @on-load="onLoad">
      <template slot="menuLeft">
        <el-button type="warning" icon="el-icon-back" size="small" @click.stop="goBack()">返回列表</el-button>
      </template>
      <!--      <template slot="isRead" slot-scope="scope">-->
      <!--        <el-switch-->
      <!--          :value="scope.row.isRead"-->
      <!--          disabled="disabled"-->
      <!--        ></el-switch>-->
      <!--    </template>-->
    </avue-crud>
  </basic-container>
</template>

<script>
import {getList, getDetail, add, update, remove} from "@/api/service/tzqs";
import {mapGetters} from "vuex";

export default {
  props: {
    tzId: {
      type: String,
      default: '0'
    },
    showMainView: {
      type: Function,
      default: null
    }
  },
  data() {
    return {
      form: {},
      query: {},
      loading: true,
      page: {
        pageSize: 50,
        currentPage: 1,
        total: 0
      },
      selectionList: [],
      option: {
        height: 'auto',
        calcHeight: 210,
        searchShow: true,
        searchMenuSpan: 6,
        menu: false,
        tip: false,
        border: true,
        index: true,
        viewBtn: true,
        selection: true,
        column: [
          {
            label: "主键",
            prop: "id",
            rules: [{
              required: true,
              message: "请输入主键",
              trigger: "blur"
            }],
            hide: true,
            display: false,
          },
          {
            label: "签收用户",
            // prop: "studentName",
            prop: "name",
            overHidden: true,
            rules: [{
              required: true,
              message: "请输入签收用户",
              trigger: "blur"
            }],
            // type: "select",
            // dicUrl: "/api/school-service/xs/select",
            // props: {
            //   label: "xsName",
            //   value: "id"
            // },
          },
          {
            label: "学生姓名",
            prop: "studentName",
            // prop: "name",
            overHidden: true,
            rules: [{
              required: true,
              message: "请输入签收用户",
              trigger: "blur"
            }],
            // type: "select",
            // dicUrl: "/api/school-service/xs/select",
            // props: {
            //   label: "xsName",
            //   value: "id"
            // },
          },
          {
            label: "签收时间",
            prop: "notificationTime",
            type: "datetime",
            format: "yyyy-MM-dd HH:mm:ss",
            valueFormat: "yyyy-MM-dd HH:mm:ss",
            overHidden: true,
            rules: [{
              required: true,
              message: "请输入签收时间",
              trigger: "blur"
            }]
          },
          {
            label: "是否已读",
            prop: "isRead",
            overHidden: true,
            span: 24,
            rules: [{
              required: true,
              message: "请输入是否已读",
              trigger: "blur"
            }],
            maxlength: 20,
            showWordLimit: true,
            editDisabled: false
          },
          // {
          //   label: "是否已读",
          //   prop: "isRead",
          //   value: "true",
          //   type: "switch",
          //   slot: true,
          //   dicData: [
          //     {
          //       label: "已读",
          //       value: "true",
          //     },
          //     {
          //       label: "未读",
          //       value: "false",
          //     },
          //   ],
          //   overHidden: true,
          //   align: "center",
          //   rules: [{
          //     required: true,
          //     message: "是否已读",
          //     trigger: "blur"
          //   }]
          // },
        ]
      },
      data: []
    };
  },
  computed: {
    ...mapGetters(["permission"]),
    permissionList() {
      return {
        addBtn: false,
        viewBtn: false,
        delBtn: false,
        editBtn: false
      };
    },
    ids() {
      let ids = [];
      this.selectionList.forEach(ele => {
        ids.push(ele.id);
      });
      return ids.join(",");
    }
  },
  methods: {
    goBack() {
      this.showMainView();
    },
    rowSave(row, done, loading) {
      row.notificationid = this.tzId;
      add(row).then(() => {
        done();
        this.onLoad(this.page);
        this.$message({
          type: "success",
          message: "操作成功!"
        });
      }, error => {
        window.console.log(error);
        loading();
      });
    },
    rowUpdate(row, index, done, loading) {
      row.notificationid = this.tzId;
      update(row).then(() => {
        done();
        this.onLoad(this.page);
        this.$message({
          type: "success",
          message: "操作成功!"
        });
      }, error => {
        window.console.log(error);
        loading();
      });
    },
    rowDel(row) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          return remove(row.id);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        });
    },
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          return remove(this.ids);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          this.$refs.crud.toggleSelection();
        });
    },
    beforeOpen(done, type) {
      if (["edit", "view"].includes(type)) {
        getDetail(this.form.id).then(res => {
          this.form = res.data.data;
        });
      }
      done();
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    onLoad(page, params = {}) {
      this.loading = true;
      params.id = this.tzId; //通知id
      getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
        const data = res.data.data;
        this.page.total = data.total;
        data.records.forEach(item => {
          item.isRead = item.isRead ? '是' : '否'
        })
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
      });
    }
  }
};
</script>

<style></style>
